package com.klod.news.utils

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV


/**
 * 替换成MMKV
 */
object MMKVUtils {
    private val mmkv: MMKV = MMKV.defaultMMKV()


//
//    fun getUser():LoginBean.User? {
//        val json = mmkv.decodeString(Contacts.UserInfo, null)
//        return json?.fromJson(LoginBean.User::class.java)
//    }
//
//
//    fun setUser(userInfo: LoginBean.User) {
//        putString(Contacts.UserInfo,userInfo.toJson())
//    }
//
//    fun getLocationInfo():LocationDataInfo? {
//        val json = mmkv.decodeString(Contacts.LocationInfo, null)
//        return json?.fromJson(LocationDataInfo::class.java)
//    }
//    fun setLocationInfo(locationDataInfo: LocationDataInfo) {
//        putString(Contacts.LocationInfo,locationDataInfo.toJson())
//    }
//
//   fun getToken(): String?{
//        val json = mmkv.decodeString(Contacts.TokenInfo, null)
//        return json
//    }
//    fun setToken(tokenInfo: String){
//        putString(Contacts.TokenInfo,tokenInfo)
////        putString(Contacts.TokenString,tokenInfo.)
//    }

    fun removeValueForKey(key: String) {
        mmkv.removeValueForKey(key)
    }

    fun saveBoolean(key: String?, value: Boolean) {
        mmkv.encode(key, value)
    }

    fun getBoolean(key: String?): Boolean {
        return mmkv.decodeBool(key, false)
    }

    fun getBoolean(key: String?, def: Boolean): Boolean {
        return mmkv.decodeBool(key, def)
    }

    fun saveString(key: String?, value: String?) {
        mmkv.encode(key, value)
    }

    fun getString(key: String?) = mmkv.decodeString(key, "") ?: ""


    fun getString(key: String?, def: String?) = mmkv.decodeString(key, def) ?: ""


    fun saveInt(key: String?, value: Int) {
        mmkv.encode(key, value)
    }

    fun getInt(key: String?) = mmkv.decodeInt(key, 0)

    fun getInt(key: String?, value: Int) = mmkv.decodeInt(key, value)


    fun saveLong(key: String?, value: Long) {
        mmkv.encode(key, value)

    }

    fun getLong(key: String?): Long {
        return mmkv.decodeLong(key, 0)
    }

    fun saveList(key: String?, dataList: List<*>?) {
        val gson = Gson()
        val json = gson.toJson(dataList)
        mmkv.encode(key, json)
    }

    fun getList(key: String?): MutableList<String> {
        val dataList: MutableList<String>
        val json = mmkv.decodeString(key, null) ?: return mutableListOf()
        val gson = Gson()
        dataList = gson.fromJson(json, object : TypeToken<MutableList<String?>?>() {}.type)
        return dataList
    }

}